package leetcode.n2021;

import java.util.List;

/**
 * 合并两个有序链表
 * https://leetcode-cn.com/problems/merge-two-sorted-lists/
 * @author Chaos
 * @date 2021/6/2
 */
public class Solution {

    private static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) {this.val = val;}
        ListNode(int val, ListNode next) {this.val = val; this.next = next;}
    }

    public static void main(String[] args) {
        Solution test = new Solution();

        ListNode l1 = new ListNode(1,null);
        ListNode l2 = new ListNode( );
        System.out.println(test.test(l1,l2));
    }

    public ListNode test(ListNode l1, ListNode l2) {
        ListNode result;

        if (l1 == null) {
            result = l2;
            return result;
        }

        if (l2 == null) {
            result = l1;
            return result;
        }

        if (l1.val > l2.val) {
            ListNode node = l1;
            l1 = l2;
            l2 = node;
        }

        result = l1;

        while (l1 != null) {
            System.out.println("l1:" + l1.val);
            while (l2 != null) {
                System.out.println("l2:" + l2.val);

                if (l1.next == null) {
                    if (l1.val <= l2.val) {
                        ListNode node = new ListNode(l2.val, null);
                        l2 = l2.next;
                        l1.next = node;
                    }
                    break;
                }

                if (l1.val <= l2.val && l1.next.val > l2.val) {
                    ListNode node = new ListNode(l2.val,l1.next);
                    l2 = l2.next;
                    l1.next = node;
                }
                break;
            }
            l1 = l1.next;
        }

        return result;
    }
}
